using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using YachtCaptain.Game_States;

namespace YachtCaptain
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        //GAME STATES
        IBaseState m_CurrentState;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        
        //MOUSE STUFF
        MouseState mouseState;

        //TEXTURES
        List<Texture2D> m_LTextures;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;            

            m_LTextures = new List<Texture2D>();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            m_CurrentState = CMainMenuState.GetInstance();
            m_CurrentState.Initialize(this);
            base.Initialize();

            //MOUSE STUFF
            mouseState = Mouse.GetState();
            Mouse.WindowHandle = Window.Handle;
            IsMouseVisible = true;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // TODO: use this.Content to load your game content here
            m_CurrentState.LoadContent();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            Content.Unload();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            //MOUSE STUFF
            
            mouseState = Mouse.GetState();
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            //GET CURRENT STATE INPUT
            m_CurrentState.Input();
            m_CurrentState.Update();
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            m_CurrentState.Render(spriteBatch);

            //DRAWING THE MOUSE POSITION
            Vector2 pos = new Vector2(0, 0);
            //MOUSE STUFF
            
            SpriteFont newFont = Content.Load<SpriteFont>("Courier New");
            spriteBatch.DrawString(newFont, "X: ", pos, Color.Black);
            spriteBatch.DrawString(newFont, mouseState.X.ToString(), new Vector2(pos.X + 30, pos.Y), Color.Black);
            spriteBatch.DrawString(newFont, "Y: ", new Vector2(pos.X, pos.Y + 30), Color.Black);
            spriteBatch.DrawString(newFont, mouseState.Y.ToString(), new Vector2(pos.X + 30, pos.Y + 30), Color.Black);


            spriteBatch.End();
            base.Draw(gameTime);
        }

        public void ChangeState(IBaseState newState)
        {
            m_CurrentState.Shutdown();
            m_CurrentState = newState;
            m_CurrentState.Initialize(this);

            //UPDATE THE MOUSE HANDLE
            Mouse.WindowHandle = Window.Handle;
        }

        //LOADING MY STATE SPECIFIC CONTENT
        public List<Texture2D> LoadMainMenuContent()
        {
            m_LTextures.Clear();

            Texture2D temptexture;
            //BackGround
            temptexture = Content.Load<Texture2D>("Ocean");
            m_LTextures.Add(temptexture);

            return m_LTextures;
        }

        public List<Texture2D> LoadGameplayContent()
        {
            m_LTextures.Clear();

            Texture2D temptexture;
            //BackGround
            temptexture = Content.Load<Texture2D>("MainMenu");            
            m_LTextures.Add(temptexture);
            temptexture = Content.Load<Texture2D>("Boat");
            m_LTextures.Add(temptexture);

            return m_LTextures;
        }

        public MouseState GetMouseState()
        {
            return mouseState;
        }
    }
}
